package com.tangyuan.live.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.common.util.StringUtils;
import com.tangyuan.feignapis.user.IUserFeignClient;
import com.tangyuan.live.entity.dto.LiveCommentDto;
import com.tangyuan.live.service.LiveCommentService;
import com.tangyuan.live.mapper.LiveCommentMapper;
import com.tangyuan.model.User;
import com.tangyuan.model.live.LiveComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class LiveCommentServiceImpl extends ServiceImpl<LiveCommentMapper, LiveComment>
    implements LiveCommentService{

    @Autowired
    private LiveCommentMapper liveCommentMapper;

    @Autowired
    private IUserFeignClient userFeignClient;

    @Override
    public Result addLiveComment(Long userId, LiveCommentDto dto) {
        if (userId == null || dto == null || dto.getRoomId() == null || StringUtils.isBlank(dto.getContent())){
            return Result.build(null, ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        User user = userFeignClient.getUserById(userId);
        if (user == null){
            return Result.build(null, ResultCodeEnum.LOGIN_ACCOUNT_ERROR);
        }
        LiveComment liveComment = new LiveComment();
        liveComment.setRoomId(dto.getRoomId());
        liveComment.setUserId(userId);
        liveComment.setUserName(user.getName());
        liveComment.setLiveComment(dto.getContent());
        liveComment.setCreateTime(new Date());
        liveCommentMapper.insert(liveComment);
        return Result.ok();
    }
}




